package com.weimob.mcloud.wechatwork.customer.service.impl.statistic;

import com.alibaba.dubbo.config.annotation.Reference;
import com.weimob.mcloud.wechatwork.common.utils.JsonUtil;
import com.weimob.mcloud.wechatwork.core.base.MultiResponse;
import com.weimob.mcloud.wechatwork.core.base.SingleRequest;
import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphere;
import com.weimob.mcloud.wechatwork.core.entity.relation.statistic.ExternaluserStatistics;
import com.weimob.mcloud.wechatwork.core.service.privatesphere.PrivateSphereService;
import com.weimob.mcloud.wechatwork.core.service.statistic.StatisticsService;
import com.weimob.mcloud.wechatwork.core.service.statistic.request.StatisticsQueryInfo;
import com.weimob.mcloud.wechatwork.customer.service.user.base.BasicInfo;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.statistic.ExternaluserStatisticsDTO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.req.statistic.StatisticsQueryInfoDTO;
import com.weimob.mcloud.wechatwork.customer.service.user.service.statistic.StatisticExport;
import com.weimob.soa.common.response.SoaResponse;
import com.weimob.soa.common.utils.SoaUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class StatisticExportImpl implements StatisticExport {

    @Reference
    private StatisticsService statisticsService;
    @Reference
    private PrivateSphereService privateSphereService;

    /**
     * 企微助手产品id
     */
    @Value("${aewc.productId:203}")
    private Long aewcProductId;

    @Override
    public SoaResponse<List<ExternaluserStatisticsDTO>, Void> getExternalUserStatistics(StatisticsQueryInfoDTO request) {
        PrivateSphere privateSphere = this.getPrivateSphereByBasicInfo(request.getBasicInfo());
        if(privateSphere == null || privateSphere.getCorpId() == null){
            log.info("getExternalUserStatistics 获取privateSphere失败 basicInfo:{}", JsonUtil.obj2Str(request));
            return SoaUtil.ok(new ArrayList<>());
        }
        StatisticsQueryInfo statisticsQueryInfo = new StatisticsQueryInfo();
        statisticsQueryInfo.setStatisticDate(request.getStatisticDate());
        statisticsQueryInfo.setDataType(request.getDataType());
        SingleRequest<StatisticsQueryInfo> statisticsQueryInfoSingleRequest = new SingleRequest<>(statisticsQueryInfo);
        statisticsQueryInfoSingleRequest.setPrivateSphere(privateSphere);
        MultiResponse<ExternaluserStatistics> externaluserStatistics = SoaUtil.unpack(statisticsService.getExternaluserStatistics(statisticsQueryInfoSingleRequest));
        List<ExternaluserStatistics> dataList = externaluserStatistics.getDataList();
        List<ExternaluserStatisticsDTO> result = new ArrayList<>();
        for (ExternaluserStatistics data : dataList) {
            ExternaluserStatisticsDTO dto = new ExternaluserStatisticsDTO();
            BeanUtils.copyProperties(data,dto);
            result.add(dto);
        }
        return SoaUtil.ok(result);
    }

    private PrivateSphere getPrivateSphereByBasicInfo(BasicInfo basicInfo) {
        PrivateSphere privateSphere = new PrivateSphere();
        if (basicInfo != null) {
            BeanUtils.copyProperties(basicInfo, privateSphere);
        }
        if (null == privateSphere.getProductId()) {
            privateSphere.setProductId(aewcProductId);
        }
        PrivateSphere privateSphereResult = null;
        try {
            privateSphereResult = SoaUtil.unpack(privateSphereService.fetch(privateSphere));
        }catch (Exception e){
            log.error("getPrivateSphereByBasicInfo error",e);
        }
        return privateSphereResult;
    }
}
